package cn.kgc.springboot.vue.controller;

import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.entity.Student;
import cn.kgc.springboot.vue.exception.BusinessException;
import cn.kgc.springboot.vue.response.ResponseResult;
import cn.kgc.springboot.vue.service.StudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 课工场
 * @date 2024/12/27
 * @description
 */
@RestController
@RequestMapping("student")
public class StudentController {

    @Resource
    private StudentService studentService;


    // 分页
    @PostMapping("page")
    public ResponseResult studentPage(@RequestBody QueryCondition<Student> queryCondition) {
        return  studentService.studentPage(queryCondition);
    }


    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateStudent(@RequestBody Student student) {
        try {
            studentService.saveOrUpdate(student);
            return  ResponseResult.success();
        }catch (DuplicateKeyException e){
            throw  new BusinessException("学号"+student.getStudentNo()+"重复");
        }
    }



    // 删除学生信息
    @DeleteMapping("delete/{id}")
    public ResponseResult deleteStudent(@PathVariable Integer id){
        studentService.removeById(id);
        return ResponseResult.success();
    }

    @PostMapping("import")
    public ResponseResult importStudent(@RequestParam("file") MultipartFile file){

        return  studentService.inportStudent(file);
    }

    @GetMapping("export")
    public void exportStudents(String studentNo,String clazz, HttpServletResponse response){
       studentService.exportStudent(studentNo,clazz,response);
    }

}
